package com.authsphere.plugin.entity;

import com.authsphere.plugin.exception.DecryptAuthenticationException;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @param getterMethod getter method
 * @param setterMethod setter method
 * @param field        crypt field
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/28 15:22
 */
public record ObjectCryptFieldBean(Object object, Method getterMethod, Method setterMethod,
                                   Field field) implements CryptFieldBean {

    @Override
    public String getFieldName() {
        return this.field().getName();
    }

    @Override
    public String getFieldValue() {
        try {
            return (String) this.getterMethod().invoke(object());
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new DecryptAuthenticationException("获取参数异常", e);
        }
    }

    @Override
    public void setFieldValue(String newValue) {
        try {
            this.setterMethod().invoke(this.object(), newValue);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new DecryptAuthenticationException("参数设置错误");
        }
    }
}
